package demo2;

import java.util.Objects;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //重写Object类的equals方法，然后比较：
    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(this == obj){
            return true;//判断是否引用同一个对象
        }
        if(!(obj instanceof Person)){
            return false;//判断是否是Person类对象
        }
        Person person = (Person)obj;//向下转型 比较属性值
        return this.name.equals(person.name) && this.age == person.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name,age);
    }
}


public class Test {
    public static void main(String[] args) {
        Person p1 = new Person("张三", 20);
        Person p2 = new Person("张三", 20);

        int a = 10;
        int b = 10;

        System.out.println(a == b);//true
        System.out.println(p1 == p2);//false
        System.out.println(p1.equals(p2));//true

        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
    }
}
